using BlazorAdmin.Interface;
using BlazorAdmin.Models;
using BlazorAdmin.Services.DbService;
using BootstrapBlazor.Components;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Pangeo.Admin.Services
{
    /// <summary>
    /// EmailTasks 服务
    /// </summary>
    public class EmailTasksService : BaMgmtDbService, IEmailTasks
    {
        public EmailTasksService(IPetaPoco db) : base(db) { }

        #region 默认生成的代码

        /// <summary>
        /// 保存、更新
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>错误消息</returns>
        public async Task<string> Save(EmailTask entity)
        {
            using var db = Create();
            bool isUpdate = entity.Id > 0;

            try
            {
                if (isUpdate)
                {
                    //var prvEntity = db.FirstOrDefault<EmailTasks>($"where id = {entity.id}");

                    // 更新指定字段
                    //prvEntity.updater = prvEntity.updater;
                    //prvEntity.updatetime = prvEntity.updatetime;
                    //db.Update(prvEntity);

                    db.Update(entity);
                }
                else
                {
                    db.Insert(entity);
                }
            }
            catch(Exception ex)
            {
                return $"保存出错，{ex.Message}";
            }

            return string.Empty;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="query">查询参数集合</param>
        /// <returns>分页数据</returns>
        public async Task<QueryData<EmailTask>> Query(AppQueryOption query)
        {
            var strSql = @$"select * from EmailTasks where 1=1";

            query.Sql = new(strSql, new { param = "value" });

            //注意：多表关联查询，需删除下面这行（默认false），否则过滤、查询会被当做内联查询，影响效率
            query.Sql.IsSingleTable = true;

            //注意：如果只查单表，且没有任何自定义参数，可以删除上面所有代码，直接return PageQueryAsync即可
            return await PageQueryAsync<EmailTask>(query);
        }

        /// <summary>
        /// 获取实例
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>实例对象</returns>
        public async Task<EmailTask> Get(int id)
        {
            using var db = Create();
            var entity = db.FirstOrDefault<EmailTask>("where id = @0", id);

            return entity;
        }

        /// <summary>
        /// 逻辑删除实例
        /// </summary>
        /// <param name="ids">主键id集合</param>
        /// <param name="curUser">当前用户</param>
        /// <returns></returns>
        public async Task<bool> Remove(List<int> ids, string curUser)
        {
            var strSql = $"set status = @0, updater = @1, updatetime = now() where id in ({string.Join(",", ids)})";
            using var db = Create();
            var rows = db.Update<EmailTask>(strSql, EnumStatus.Del.ToString(), curUser);

            return rows > 0;
        }

        /// <summary>
        /// 物理删除实例
        /// </summary>
        /// <param name="ids">主键id集合</param>
        /// <returns></returns>
        public async Task<bool> Delete(List<int> ids)
        {
            using var db = Create();
            var rows = db.Delete<EmailTask>($"where id in ({string.Join(",", ids)})");

            return rows > 0;
        }

        #endregion
    }
}
